import unittest
from pathlib import Path
import numpy as np
import cv2
import sys

# 导入profile_decorator
sys.path.append('{}/'.format(Path(__file__).parent.parent))

from xinfer_yolo.detect.preprocess import preprocess, preprocess_cpy # noqa
from profile_decorator import profile_decorator # noqa

default_image_path = Path(__file__).parent.parent / 'images' / 'bus.jpg'
class TestPreprocess(unittest.TestCase):
    def __init__(self,
                 *args,
                 image: str | Path | np.ndarray = default_image_path,
                 dst_width: int = 640,
                 dst_height: int = 640) -> None:
        super().__init__(*args)

        if isinstance(image, str):
            image = Path(image).absolute()
        if isinstance(image, Path):
            if not image.exists():
                raise FileNotFoundError(f'路径\'{image}\'不存在')
            image = cv2.imdecode(np.fromfile(image, dtype=np.uint8), cv2.IMREAD_COLOR)

        dst = np.zeros(shape=(3, dst_height, dst_width), dtype=np.float32)
        self.image = image
        self.dst = dst
        self.width = dst_width
        self.height = dst_height

    @profile_decorator
    def test_basic(self) -> None:
        preprocess(self.image, self.dst)

    @profile_decorator
    def test_preprocess_cpy(self):
        preprocess_cpy(self.image, self.dst, self.width, self.height)
